package com.text;

import com.dao.user.UserMapper;
import com.entity.User;
import com.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import java.text.SimpleDateFormat;
import java.util.*;

public class UserMapperText {
    static Logger logger = Logger.getLogger(UserMapperText.class);
    public void testAdd(){
        logger.debug("testAdd !================");

        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            User user = new User();
            user.setUserCode("test001");
            user.setUserName("测试用户001");
            user.setUserPassword("1234567");
            Date birthday = new SimpleDateFormat("yyyy-MM-dd").parse("1984-12-12");
            user.setBirthday(birthday);
            user.setAddress("地址测试");
            user.setGender(1);
            user.setPhone("13688783697");
            user.setUserRole(1);
            user.setCreatedBy(1);
            user.setCreationDate(new Date());
            count = sqlSession.getMapper(UserMapper.class).add(user);

            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("testAdd count:" +count);
    }

    public  void testGetUserList(){
        SqlSession sqlSession = null;
        List<User> userList = new ArrayList<User>();
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            //调用selectList方法执行查询操作
            userList = sqlSession.selectList("com.czh.dao.user.UserMapper.getUserList");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (User user : userList){
             logger.debug("testGetUserList userCode:" + user.getUserCode() + "and userName:" + user.getUserName());
        }
    }
    public  void testGetUserLists(){
        SqlSession sqlSession = null;
        List<User> userList = new ArrayList<User>();
        try {
//            Map<String ,String > userMap = new HashMap<String,String>();
//            userMap.put("uName","李");
//            userMap.put("uRole","3");
//            userList = sqlSession.getMapper(UserMapper.class).getUserListByMap(userMap);
            sqlSession = MyBatisUtil.createSqlSession();
            User user = new User();
            user.setUserName("李");
            user.setUserRole(3);
            //调用selectList方法执行查询操作
            userList = sqlSession.getMapper(UserMapper.class).getUserList(user);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (User user : userList){
            logger.debug("testGetUserList userCode:" + user.getUserCode() + "and userName:" + user.getUserName());
        }
    }
    public  void testGetUserListss(){
        SqlSession sqlSession = null;
        List<User> userList = new ArrayList<User>();
        try {
//            Map<String ,String > userMap = new HashMap<String,String>();
//            userMap.put("uName","李");
//            userMap.put("uRole","3");
//            userList = sqlSession.getMapper(UserMapper.class).getUserListByMap(userMap);
            sqlSession = MyBatisUtil.createSqlSession();
            User user = new User();
            user.setUserName("李");
            user.setUserRole(3);
            //调用selectList方法执行查询操作
            userList = sqlSession.getMapper(UserMapper.class).getUserList(user);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (User user : userList){
           logger.debug("testGetUserList userCode:" + user.getUserCode() + "and userName:" + user.getUserName()+"and userRole:"+user.getUserRole()+"and userRoleName:"+user.getUserRole()+"and address:"+user.getAddress());
        }
    }

    public void testModify(){
        logger.debug("testModify !================");
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            User user = new User();
            user.setId(18);
            user.setUserCode("testmodify");
            user.setUserName("测试用户修改");
            user.setUserPassword("0000000");
            Date birthday = new SimpleDateFormat("yyyy-MM-dd").parse("1980-10-10");
            user.setBirthday(birthday);
            user.setAddress("地址修改测试");
            user.setGender(2);
            user.setPhone("12312312312");
            user.setUserRole(2);
            user.setCreatedBy(1);
            user.setCreationDate(new Date());
            count = sqlSession.getMapper(UserMapper.class).modify(user);
            //模拟异常,进行回滚
            //int i = 2/0
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("testModify count:" +count);
    }

    public void textUpdatePwd(){
        logger.debug("testUpdatePwd !================");
        SqlSession sqlSession = null;
        String pwd = "888888";
        Integer id = 1;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count =sqlSession.getMapper(UserMapper.class).update(id,pwd);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("testUpdatePwd count:" +count);
    }

    public void testDelectUserById(){
        logger.debug("textDelectUserById !================");
        SqlSession sqlSession = null;
        Integer delid = 25;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count =sqlSession.getMapper(UserMapper.class).delectUserByIb(delid);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("textDelectUserById count:" +count);
    }
    public void getAddressListByUserIdText(){
        SqlSession sqlSession = null;
        List<User> userList = new ArrayList<User>();
        Integer roleId = 1;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            userList = sqlSession.getMapper(UserMapper.class).getAddressListByUserId(roleId);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (User user:userList){
            logger.debug("userList(include:addressList)====>userCode:"+user.getUserCode()+",userName:"+user.getUserName()+"");
        }
    }
    public void getUserListByRoleIdTest(){
        SqlSession sqlSession = null;
        List<User> userList = new ArrayList<User>();
        Integer roleId = 3;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            userList = sqlSession.getMapper(UserMapper.class).getUserListByRoleId(roleId);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (User user:userList){
            logger.debug("userList====>"+user.getUserName()+",Role"+user.getRole().getId()+"---"+user.getRole().getRoleCode()+"---"+user.getRole().getRoleName() );
        }
    }

    }
